package ro.imv.server.gcmDatastore;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

@SuppressWarnings("serial")
public class ClearDataStore extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		String senderId = req.getParameter("senderId");
		
		Boolean ok = false;
		int count = 0;
		try{
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			Query query = new Query("Client");
			query = query.setFilter(FilterOperator.EQUAL.of("senderId", senderId));
			
			PreparedQuery preparedQuery = datastore.prepare(query);
			Iterator<Entity> iterator = preparedQuery.asIterator();
			while (iterator.hasNext()) {
				Entity entity = (Entity) iterator.next();
				datastore.delete(entity.getKey());
				count++;
			} 
			resp.setContentType("text/plain");
			resp.getWriter().println("NK!");
			resp.getWriter().println("count="+count);
		}catch(Exception e){
			e.printStackTrace();
			resp.setContentType("text/plain");
			resp.getWriter().println("NK!");
		}
		
		
	}
}
